package com.spike.servlet;

import com.spike.model.Dept;
import com.spike.model.Emp;
import com.spike.service.DeptService;
import com.spike.service.EmpService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/empUpdatePage")
public class EmpUpdatePageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //        查询全部部门名称
        DeptService deptService = new DeptService();
        try {
            List<String> deptName = deptService.findDeptName();
            req.setAttribute("deptName",deptName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
//        根据id查询员工信息
        EmpService empService = new EmpService();
        try {
            Emp byId = empService.findById(i);
            req.setAttribute("emp",byId);
            Integer deptId = byId.getDeptId();
            Dept byId1 = deptService.findById(deptId);
            String name = byId1.getName();
            req.setAttribute("name",name);
            req.getRequestDispatcher("/WEB-INF/jsp/empUpdate.jsp").forward(req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
